package com.example.petshopping.ui.fixedprice;

import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.example.petshopping.R;
import com.example.petshopping.ui.aquarium.AquariumActivity;
import com.example.petshopping.ui.main.MainActivity;
import com.example.petshopping.ui.pet.PetActivity;
import com.google.android.material.tabs.TabLayout;

import java.util.ArrayList;
import java.util.List;

public class FixedPriceActivity extends AppCompatActivity {

    private RecyclerView productList;
    private List<FixedPriceProduct> products = new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_fixed_price);
        
        // 设置状态栏高度
        int resourceId = getResources().getIdentifier("status_bar_height", "dimen", "android");
        if (resourceId > 0) {
            int statusBarHeight = getResources().getDimensionPixelSize(resourceId);
            findViewById(R.id.status_bar_bg).getLayoutParams().height = statusBarHeight;
        }
        
        // 搜索输入框点击跳转到搜索页
        findViewById(R.id.search_input).setOnClickListener(v -> {
            Intent intent = new Intent(FixedPriceActivity.this, com.example.petshopping.ui.search.SearchActivity.class);
            startActivity(intent);
        });
        
        // 初始化商品列表
        initProductList();
        
        // 设置导航标签的点击监听
        setupNavigation();
        
        // 设置底部导航栏
        setupBottomNavigation();
    }
    
    private void initProductList() {
        // 准备示例数据
        prepareProductData();
        
        // 初始化RecyclerView
        productList = findViewById(R.id.fixed_price_product_list);
        productList.setLayoutManager(new GridLayoutManager(this, 2)); // 2列网格布局
        
        // 创建适配器
        FixedPriceProductAdapter adapter = new FixedPriceProductAdapter(products);
        productList.setAdapter(adapter);
    }
    
    private void prepareProductData() {
        // 添加一些示例商品数据
        products.add(new FixedPriceProduct("优质宠物食盆", "¥39.9", "¥59.9", "已售 135", "顶级宠物用品店", "上海"));
        products.add(new FixedPriceProduct("猫咪智能玩具", "¥89", "¥129", "已售 98", "萌宠乐园", "北京"));
        products.add(new FixedPriceProduct("鱼缸过滤器", "¥158", "¥199", "已售 76", "水族之家", "广州"));
        products.add(new FixedPriceProduct("宠物专用沐浴露", "¥45", "¥68", "已售 210", "宠物清洁用品", "深圳"));
        products.add(new FixedPriceProduct("猫抓板组合装", "¥79", "¥99", "已售 189", "猫咪乐园", "杭州"));
        products.add(new FixedPriceProduct("狗狗训练零食", "¥29.9", "¥39.9", "已售 320", "汪星训练营", "成都"));
    }
    
    private void setupNavigation() {
        TabLayout navMenu = findViewById(R.id.nav_menu);
        navMenu.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                int position = tab.getPosition();
                // 如果不是点击当前页面对应的标签，则进行页面跳转
                if (position != 3) { // 一口价标签的位置是3
                    switch (position) {
                        case 0: // 首页
                            startActivity(new Intent(FixedPriceActivity.this, MainActivity.class));
                            overridePendingTransition(0,0);
                            finish();
                            break;
                        case 1: // 宠物
                            startActivity(new Intent(FixedPriceActivity.this, PetActivity.class));
                            overridePendingTransition(0,0);
                            finish();
                            break;
                        case 2: // 水族
                            startActivity(new Intent(FixedPriceActivity.this, AquariumActivity.class));
                            overridePendingTransition(0,0);
                            finish();
                            break;
                        case 4: // 同城
                            startActivity(new Intent(FixedPriceActivity.this, com.example.petshopping.ui.samecity.SameCityActivity.class));
                            overridePendingTransition(0,0);
                            finish();
                            break;
                    }
                }
            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {
                // 不需要处理
            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {
                // 不需要处理
            }
        });
        
        // 设置默认选中的标签为一口价
        navMenu.getTabAt(3).select();
    }
    
    private void setupBottomNavigation() {
        // 获取底部导航栏
        com.google.android.material.bottomnavigation.BottomNavigationView bottomNav = findViewById(R.id.bottom_nav);
        
        // 设置当前选中项为首页(因为一口价是顶部标签，底部没有对应标签)
        bottomNav.setSelectedItemId(R.id.navigation_home);
        
        // 设置点击监听器
        bottomNav.setOnNavigationItemSelectedListener(item -> {
            switch (item.getItemId()) {
                case R.id.navigation_home:
                    // 跳转到首页
                    startActivity(new Intent(this, MainActivity.class));
                    overridePendingTransition(0,0);
                    finish();
                    return true;
                case R.id.navigation_category:
                    // 跳转到分类页面
                    startActivity(new Intent(this, com.example.petshopping.ui.category.CategoryActivity.class));
                    overridePendingTransition(0,0);
                    finish();
                    return true;
                case R.id.navigation_message:
                    // 跳转到消息页面
                    startActivity(new Intent(this, com.example.petshopping.ui.message.MessageActivity.class));
                    overridePendingTransition(0,0);
                    finish();
                    return true;
                case R.id.navigation_profile:
                    // 跳转到我的页面
                    startActivity(new Intent(this, com.example.petshopping.ui.profile.ProfileActivity.class));
                    overridePendingTransition(0,0);
                    finish();
                    return true;
            }
            return false;
        });
        
        // 设置中间按钮点击事件
        findViewById(R.id.center_button).setOnClickListener(v -> {
            // 中间按钮点击事件
        });
        
        // 设置参与按钮点击事件
        findViewById(R.id.participate_btn).setOnClickListener(v -> {
            // 参与按钮点击事件
        });
    }
    
    // 一口价商品数据模型
    private static class FixedPriceProduct {
        String name;
        String price;
        String originalPrice;
        String salesCount;
        String storeName;
        String location;
        
        public FixedPriceProduct(String name, String price, String originalPrice, String salesCount, String storeName, String location) {
            this.name = name;
            this.price = price;
            this.originalPrice = originalPrice;
            this.salesCount = salesCount;
            this.storeName = storeName;
            this.location = location;
        }
    }
    
    // 一口价商品列表适配器
    private class FixedPriceProductAdapter extends RecyclerView.Adapter<FixedPriceProductAdapter.ViewHolder> {
        
        private List<FixedPriceProduct> productList;
        
        public FixedPriceProductAdapter(List<FixedPriceProduct> productList) {
            this.productList = productList;
        }
        
        @NonNull
        @Override
        public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
            View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_fixed_price_product, parent, false);
            return new ViewHolder(view);
        }
        
        @Override
        public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
            FixedPriceProduct product = productList.get(position);
            
            // 设置商品信息
            holder.productName.setText(product.name);
            holder.productPrice.setText(product.price);
            holder.productOriginalPrice.setText(product.originalPrice);
            holder.salesCount.setText(product.salesCount);
            holder.storeName.setText(product.storeName);
            holder.location.setText(product.location);
            
            // 使用占位图，这里可以根据不同位置设置不同的图片以区分
            int[] productImages = {R.drawable.pet_4, R.drawable.pet_3, R.drawable.pet_2, R.drawable.pet_5};
            holder.productImage.setImageResource(productImages[position % productImages.length]);
        }
        
        @Override
        public int getItemCount() {
            return productList.size();
        }
        
        class ViewHolder extends RecyclerView.ViewHolder {
            ImageView productImage;
            TextView productName;
            TextView productPrice;
            TextView productOriginalPrice;
            TextView salesCount;
            TextView storeName;
            TextView location;
            
            public ViewHolder(@NonNull View itemView) {
                super(itemView);
                
                productImage = itemView.findViewById(R.id.product_image);
                productName = itemView.findViewById(R.id.product_name);
                productPrice = itemView.findViewById(R.id.product_price);
                productOriginalPrice = itemView.findViewById(R.id.product_original_price);
                salesCount = itemView.findViewById(R.id.sales_count);
                storeName = itemView.findViewById(R.id.store_name);
                location = itemView.findViewById(R.id.location);
            }
        }
    }
}